/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import nhadat.tindang.DientichkhuonvienType;
import nhadat.tindang.DientichxaydungType;
import nhadat.tindang.GiatienType;
import nhadat.tindang.KhuvucType;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.ChieudaikhuonvienType;
import nhadat.tindangfull.ChieudaixaydungType;
import nhadat.tindangfull.ChieungangkhuonvienType;
import nhadat.tindangfull.ChieungangxaydungType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.GiatienfullType;
import nhadat.tindangfull.KhuvucfullType;
import nhadat.tindangfull.TindangfullType;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author DUNGND60190
 */
public class StaxParser {

    public StaxParser() {
    }

//    public static void main(String[] args) {
//        String inputXML = "web/xml/TblLoaiTin.xml";
//        String expression = "//dsloaitin/loaitin[tenloaitin='Cần Bán']/maloaitin";
//
//
//        //readXMLbyIteratorStAX(inputXML);
//        //Date date = new Date();
//        //System.out.println(date + "");
//        StaxParser test = new StaxParser();
//        String maloainha = test.readXMLgetMaByTen(expression, inputXML);
//        System.out.println(maloainha);
//    }

    public ArrayList<TindangfullType> readXMLtoTindangFullTypebyStAX(String inputXML) {
        FileInputStream input = null;

        try {
            XMLInputFactory xif = XMLInputFactory.newFactory();
            input = new FileInputStream(inputXML);
            XMLEventReader eventReader = xif.createXMLEventReader(input);
            int i = 0;
            ArrayList<TindangfullType> listTindang = new ArrayList<TindangfullType>();

            //Attribute of TindangType
            String tindangStatus = null;
            String matindang = null;
            String tenloainha = null;
            String tenloaigiayto = null;
            String thongtinchitiet = null;
            String cautruc = null;
            String dvddientichkhuonvien = null;
            String solieukhuonvien = null;
            String dvdchieungangkhuonvien = null;
            String solieuchieungangkhuonvien = null;
            String dvdchieudaikhuonvien = null;
            String solieuchieudaikhuonvien = null;
            String dvddientichxaydung = null;
            String solieuxaydung = null;
            String dvdchieungangxaydung = null;
            String solieuchieungangxaydung = null;
            String dvdchieudaixaydung = null;
            String solieuchieudaixaydung = null;
            String diachi = null;
            String quanhuyen = null;
            String tinhthanh = null;
            String solieugiatien = null;
            String dvdgiatien = null;
            String hinhanh = null;
            String tieudetin = null;
            String tenloaitin = null;
            String nguoidangtin = null;
            String ngaydangtin = null;
            String lancuoicapnhat = null;
            String soluotxem = null;
            String isActive = null;

            //END
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                 //System.out.println("CHAY LAI TAI DAY");
                if (event.isStartElement()) {

                    if (event.asStartElement().getName().getLocalPart().equals("tindang")) {
                        StartElement element = (StartElement) event;
                        Iterator iter = element.getAttributes();
                        while (iter.hasNext()) {
                            Attribute attr = (Attribute) iter.next();
                            tindangStatus = attr.getValue();
                            //System.out.println(tindangStatus + " loop = " + i);

                        }
                        //event = eventReader.nextEvent();
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("matindang")) {
                        event = eventReader.nextEvent();
                        matindang = event.asCharacters().getData();
                        //System.out.println(matindang + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloainha")) {
                        event = eventReader.nextEvent();
                        tenloainha = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(tenloainha + " tenloainha loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloaigiayto")) {
                        event = eventReader.nextEvent();
                        tenloaigiayto = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(tenloaigiayto + " tenloaigiayto loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("cautruc")) {
                        event = eventReader.nextEvent();
                        cautruc = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(cautruc + " cautruc loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("thongtinchitiet")) {
                        event = eventReader.nextEvent();
                        thongtinchitiet = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(thongtinchitiet + " thongtinchitiet loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvddientichkhuonvien")) {
                        event = eventReader.nextEvent();
                        dvddientichkhuonvien = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                       // System.out.println(dvddientichkhuonvien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieukhuonvien = event.asCharacters().getData();
                            //System.out.println(solieukhuonvien + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdchieungangkhuonvien")) {
                        event = eventReader.nextEvent();
                        dvdchieungangkhuonvien = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvdchieungangkhuonvien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuchieungangkhuonvien = event.asCharacters().getData();
                            //System.out.println(solieuchieungangkhuonvien + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdchieudaikhuonvien")) {
                        event = eventReader.nextEvent();
                        dvdchieudaikhuonvien = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvdchieudaikhuonvien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuchieudaikhuonvien = event.asCharacters().getData();
                            //System.out.println(solieuchieudaikhuonvien + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvddientichxaydung")) {
                        event = eventReader.nextEvent();
                        dvddientichxaydung = event.asCharacters().getData();
                        //System.out.println(dvddientichxaydung + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuxaydung = event.asCharacters().getData();
                            //System.out.println(solieuxaydung + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdchieungangxaydung")) {
                        event = eventReader.nextEvent();
                        dvdchieungangxaydung = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvdchieungangxaydung + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuchieungangxaydung = event.asCharacters().getData();
                            //System.out.println(solieuchieungangxaydung + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdchieudaixaydung")) {
                        event = eventReader.nextEvent();
                        dvdchieudaixaydung = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvdchieudaixaydung + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuchieudaixaydung = event.asCharacters().getData();
                            //System.out.println(solieuchieudaixaydung + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("diachi")) {
                        event = eventReader.nextEvent();
                        diachi = event.asCharacters().getData();
                        //System.out.println(diachi + " loop = " + i);
                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("quanhuyen")) {
                        event = eventReader.nextEvent();
                        quanhuyen = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(quanhuyen + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("tinhthanh")) {
                            event = eventReader.nextEvent();
                            tinhthanh = event.asCharacters().getData();
                            //System.out.println(tinhthanh + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdgiatien")) {
                        event = eventReader.nextEvent();
                        dvdgiatien = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvdgiatien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieugiatien = event.asCharacters().getData();
                            //System.out.println(solieugiatien + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("hinhanh")) {
                        event = eventReader.nextEvent();
                        hinhanh = event.asCharacters().getData();
                        //System.out.println(hinhanh + " loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tieudetin")) {
                        event = eventReader.nextEvent();
                        tieudetin = event.asCharacters().getData();
                        //System.out.println(tieudetin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloaitin")) {
                        event = eventReader.nextEvent();
                        tenloaitin = event.asCharacters().getData();
                        //System.out.println(tenloaitin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("nguoidangtin")) {
                        event = eventReader.nextEvent();
                        nguoidangtin = event.asCharacters().getData();
                        //System.out.println(nguoidangtin + " loop = " + i);
                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("ngaydangtin")) {
                        event = eventReader.nextEvent();
                        ngaydangtin = event.asCharacters().getData();
                        //System.out.println(ngaydangtin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("lancuoicapnhat")) {
                        event = eventReader.nextEvent();
                        lancuoicapnhat = event.asCharacters().getData();
                        //System.out.println(lancuoicapnhat + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("soluotxem")) {
                        event = eventReader.nextEvent();
                        soluotxem = event.asCharacters().getData();
                        //System.out.println(soluotxem +  " loop = " + i);
                    } else if (event.asStartElement().getName().getLocalPart().equals("isActive")) {
                        event = eventReader.nextEvent();
                        isActive = event.asCharacters().getData();
                        //System.out.println(isActive +  " loop = " + i);
                        TindangfullType tindang = new TindangfullType();
                        i++;
                        //Set attribute of tindangtype
                        tindang.setStatus(tindangStatus);
                        tindang.setMatindang(matindang);
                        tindang.setTenloainha(tenloainha);
                        tindang.setTenloaigiayto(tenloaigiayto);
                        tindang.setCautruc(cautruc);
                        tindang.setThongtinchitiet(thongtinchitiet);

                        DientichkhuonvienfullType dtKhuonvien = new DientichkhuonvienfullType();
                        dtKhuonvien.setDvddientichkhuonvien(dvddientichkhuonvien);
                        dtKhuonvien.setSolieu(Double.parseDouble(solieukhuonvien));
                        tindang.setDientichkhuonvien(dtKhuonvien);

                        ChieungangkhuonvienType chieungangkhuonvien = new ChieungangkhuonvienType();
                        chieungangkhuonvien.setDvdchieungangkhuonvien(dvdchieungangkhuonvien);
                        chieungangkhuonvien.setSolieu(Double.parseDouble(solieuchieungangkhuonvien));
                        tindang.setChieungangkhuonvien(chieungangkhuonvien);

                        ChieudaikhuonvienType chieudaikhuonvien = new ChieudaikhuonvienType();
                        chieudaikhuonvien.setDvdchieudaikhuonvien(dvdchieudaikhuonvien);
                        chieudaikhuonvien.setSolieu(Double.parseDouble(solieuchieudaikhuonvien));
                        tindang.setChieudaikhuonvien(chieudaikhuonvien);

                        DientichxaydungfullType dtXaydung = new DientichxaydungfullType();
                        dtXaydung.setDvddientichxaydung(dvddientichxaydung);
                        dtXaydung.setSolieu(Double.parseDouble(solieuxaydung));
                        tindang.setDientichxaydung(dtXaydung);

                        ChieungangxaydungType chieungangxaydung = new ChieungangxaydungType();
                        chieungangxaydung.setDvdchieungangxaydung(dvdchieungangxaydung);
                        chieungangxaydung.setSolieu(Double.parseDouble(solieuchieungangxaydung));
                        tindang.setChieungangxaydung(chieungangxaydung);

                        ChieudaixaydungType chieudaixaydung = new ChieudaixaydungType();
                        chieudaixaydung.setDvdchieudaixaydung(dvdchieudaixaydung);
                        chieudaixaydung.setSolieu(Double.parseDouble(solieuchieudaikhuonvien));
                        tindang.setChieudaixaydung(chieudaixaydung);

                        tindang.setDiachi(diachi);
                        KhuvucfullType khuvuc = new KhuvucfullType();
                        khuvuc.setQuanhuyen(quanhuyen);
                        khuvuc.setTinhthanh(tinhthanh);
                        tindang.setKhuvuc(khuvuc);

                        GiatienfullType giatien = new GiatienfullType();
                        giatien.setSolieu(Double.parseDouble(solieugiatien));
                        giatien.setDvdgiatien(dvdgiatien);

                        tindang.setGiatien(giatien);
                        tindang.setHinhanh(hinhanh);
                        tindang.setTieudetin(tieudetin);
                        tindang.setTenloaitin(tenloaitin);
                        tindang.setNguoidangtin(nguoidangtin);
                        tindang.setNgaydangtin(ngaydangtin);
                        tindang.setLancuoicapnhat(lancuoicapnhat);
                        tindang.setSoluotxem(BigInteger.valueOf(Long.parseLong(soluotxem)));
                        tindang.setIsActive(isActive);

                        listTindang.add(tindang);

                        // continue;
                    }
                }

            }
            return listTindang;


        } catch (XMLStreamException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                input.close();
            } catch (IOException ex) {
                Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    public ArrayList<TindangType> readXMLtoTindangTypebyStAX(String inputXML) {
        FileInputStream input = null;

        try {
            XMLInputFactory xif = XMLInputFactory.newFactory();
            input = new FileInputStream(inputXML);
            XMLEventReader eventReader = xif.createXMLEventReader(input);
            int i = 0;
            ArrayList<TindangType> listTindang = new ArrayList<TindangType>();

            //Attribute of TindangType
            String tindangStatus = null;
            String matindang = null;
            String tenloainha = null;
            String tenloaigiayto = null;
            String dvddientichkhuonvien = null;
            String solieukhuonvien = null;
            String dvddientichxaydung = null;
            String solieuxaydung = null;
            String quanhuyen = null;
            String tinhthanh = null;
            String dvdgiatien = null;
            String solieugiatien = null;
            String hinhanh = null;
            String tieudetin = null;
            String tenloaitin = null;
            String nguoidangtin = null;
            String ngaydangtin = null;
            String lancuoicapnhat = null;
            String soluotxem = null;

            //END
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                // System.out.println("CHAY LAI TAI DAY");
                if (event.isStartElement()) {

                    if (event.asStartElement().getName().getLocalPart().equals("tindang")) {
                        StartElement element = (StartElement) event;
                        Iterator iter = element.getAttributes();
                        while (iter.hasNext()) {
                            Attribute attr = (Attribute) iter.next();
                            tindangStatus = attr.getValue();
                            // tindang.setStatus(tindangStatus);
                            //System.out.println(tindangStatus + " loop = " + i);

                        }

                        //event = eventReader.nextEvent();
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("matindang")) {
                        event = eventReader.nextEvent();
                        matindang = event.asCharacters().getData();
                        // tindang.setMatindang(matindang);
                        //System.out.println(matindang + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloainha")) {
                        event = eventReader.nextEvent();
                        tenloainha = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(tenloainha + " loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloaigiayto")) {
                        event = eventReader.nextEvent();
                        tenloaigiayto = event.asCharacters().getData();
                        //  tindang.setTenloainha(value);
                        //System.out.println(tenloaigiayto + " loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvddientichkhuonvien")) {
                        event = eventReader.nextEvent();
                        dvddientichkhuonvien = event.asCharacters().getData();
                        //   tindang.setDientichkhuonvien(null)
                        //System.out.println(dvddientichkhuonvien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        //event = eventReader.nextTag();
                        //event = eventReader.peek();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieukhuonvien = event.asCharacters().getData();
                            //System.out.println(solieukhuonvien + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvddientichxaydung")) {
                        event = eventReader.nextEvent();
                        dvddientichxaydung = event.asCharacters().getData();
                        //System.out.println(dvddientichxaydung + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        //event = eventReader.nextTag();
                        //event = eventReader.peek();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieuxaydung = event.asCharacters().getData();
                            //System.out.println(solieuxaydung + " loop = " + i);
                        }

                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("quanhuyen")) {
                        event = eventReader.nextEvent();
                        quanhuyen = event.asCharacters().getData();
                        //System.out.println(quanhuyen + " loop = " + i);
                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tinhthanh")) {
                        event = eventReader.nextEvent();
                        tinhthanh = event.asCharacters().getData();
                        //System.out.println(tinhthanh + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("dvdgiatien")) {
                        event = eventReader.nextEvent();
                        dvdgiatien = event.asCharacters().getData();
                        //System.out.println(dvdgiatien + " loop = " + i);
                        event = eventReader.nextEvent();
                        event = eventReader.nextTag();
                        //event = eventReader.nextTag();
                        //event = eventReader.peek();
                        if (event.asStartElement().getName().getLocalPart().equals("solieu")) {
                            event = eventReader.nextEvent();
                            solieugiatien = event.asCharacters().getData();
                           // System.out.println(solieugiatien + " loop = " + i);
                        }
                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("hinhanh")) {
                        event = eventReader.nextEvent();
                        hinhanh = event.asCharacters().getData();
                        //System.out.println(hinhanh + " loop = " + i);
                        //continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tieudetin")) {
                        event = eventReader.nextEvent();
                        tieudetin = event.asCharacters().getData();
                        //System.out.println(tieudetin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("tenloaitin")) {
                        event = eventReader.nextEvent();
                        tenloaitin = event.asCharacters().getData();
                        //System.out.println(tenloaitin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("nguoidangtin")) {
                        event = eventReader.nextEvent();
                        nguoidangtin = event.asCharacters().getData();
                        //System.out.println(nguoidangtin + " loop = " + i);
                        //  continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("ngaydangtin")) {
                        event = eventReader.nextEvent();
                        ngaydangtin = event.asCharacters().getData();
                        //System.out.println(ngaydangtin + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("lancuoicapnhat")) {
                        event = eventReader.nextEvent();
                        lancuoicapnhat = event.asCharacters().getData();
                        //System.out.println(lancuoicapnhat + " loop = " + i);
                        // continue;
                    } else if (event.asStartElement().getName().getLocalPart().equals("soluotxem")) {
                        TindangType tindang = new TindangType();
                        event = eventReader.nextEvent();
                        soluotxem = event.asCharacters().getData();
                        //System.out.println(soluotxem + " loop = " + i);
                        i++;

                        //Set attribute of tindangtype
                        tindang.setStatus(tindangStatus);
                        tindang.setMatindang(matindang);
                        tindang.setTenloainha(tenloainha);
                        tindang.setTenloaigiayto(tenloaigiayto);

                        DientichkhuonvienType dtKhuonvien = new DientichkhuonvienType();
                        dtKhuonvien.setDvddientichkhuonvien(dvddientichkhuonvien);
                        dtKhuonvien.setSolieukhuonvien(Double.parseDouble(solieukhuonvien));
                        tindang.setDientichkhuonvien(dtKhuonvien);

                        DientichxaydungType dtXaydung = new DientichxaydungType();
                        dtXaydung.setDvddientichxaydung(dvddientichxaydung);
                        dtXaydung.setSolieudientich(Double.parseDouble(solieuxaydung));
                        tindang.setDientichxaydung(dtXaydung);

                        KhuvucType khuvuc = new KhuvucType();
                        khuvuc.setQuanhuyen(quanhuyen);
                        khuvuc.setTinhthanh(tinhthanh);
                        tindang.setKhuvuc(khuvuc);

                        GiatienType giatien = new GiatienType();
                        giatien.setDvdgiatien(dvdgiatien);
                        giatien.setSolieugiatien(Double.parseDouble(solieugiatien));
                        tindang.setGiatien(giatien);

                        tindang.setHinhanh(hinhanh);
                        tindang.setTieudetin(tieudetin);
                        tindang.setTenloaitin(tenloaitin);
                        tindang.setNguoidangtin(nguoidangtin);
                        tindang.setNgaydangtin(ngaydangtin);
                        tindang.setLancuoicapnhat(lancuoicapnhat);
                        tindang.setSoluotxem(BigInteger.valueOf(Long.parseLong(soluotxem)));

                        listTindang.add(tindang);

                        // continue;
                    }
                }

            }
            return listTindang;


        } catch (XMLStreamException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                input.close();
            } catch (IOException ex) {
                Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return null;
    }

    public String readXMLgetMaByTen(String express, String inputXML) {
        try {
            // XPATH from TblLoaiNha.xml: //dsloainha/loainha[tenloainha='Nhà cấp 4']/maloainha
            // XPATH from TblLoaiGiayTo.xml: //dsloaigiayto/loaigiayto[tenloaigiayto='Sổ đỏ']/maloaigiayto
            // XPATH from TblKhuVuc.xml: //dskhuvuc/khuvuc[tinhthanh='TP. Hồ Chí Minh' and quanhuyen='Quận 1']/makhuvuc
            // XPATH from TblLoaiTin.xml: //dsloaitin/loaitin[tenloaitin='Cần Bán']/maloaitin
            
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File(inputXML));
            XPathFactory xpf =XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            String returnValue = (String) xpath.evaluate(express, doc, XPathConstants.STRING);
            if (returnValue!=null) {
                return returnValue;
            }

        } catch (XPathExpressionException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(StaxParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
